package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.HulkRepository;
import com.realtyManage.springboot.Model.Do.Hulk;
import com.realtyManage.springboot.Service.IHulkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * HulkServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/5/15
 **/
@Service
@Transactional
public class HulkServiceImpl implements IHulkService {
    @Autowired
    private HulkRepository hulkRepository;

    @Override
    public Page<Hulk> findPagedByType(String type, Integer pageNumber, Integer pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable =new PageRequest(pageNumber - 1, pageSize, sort);
        if(type!=null){
            return hulkRepository.findByType(pageable,Integer.parseInt(type));
        }else{
            return hulkRepository.findAll(pageable);
        }
    }

    @Override
    public Hulk getOne(Integer id) {
        return hulkRepository.getOne(id);
    }

    @Override
    public Hulk save(Hulk hulk) {
        return hulkRepository.save(hulk);
    }

    @Override
    public void delete(Integer id) {
        hulkRepository.deleteById(id);
    }

    @Override
    public List<String> findDistinctType() {
        return hulkRepository.findDistinctType();
    }
}
